package contador.z2;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DialogoActivity extends Activity implements android.view.View.OnClickListener {
	
	protected static final int PARAR = 0;
	protected static final int DECREMENTAR = 1;
	
	TextView cronometro;
	int tempoSeg = 3;
	int tempoMin = 0;
	Toast toast; 
	boolean continuar = true;
	AssetFileDescriptor afd;
	MediaPlayer mediaPlayer;
	
	 @Override    
    public void onCreate(Bundle b) {
		super.onCreate(b);
        setContentView(R.layout.dialogo);
        cronometro = (TextView) findViewById(R.id.cronometro);
        
        Intent it = getIntent();
		if(it != null){
			tempoMin = it.getIntExtra("min",3);
		    tempoSeg = it.getIntExtra("seg",30);
			
		}
		toast = Toast.makeText(this, "ACABOU O TEMPO", Toast.LENGTH_SHORT);
        setTitle("E o tempo passando...");
        Button fechar = (Button) findViewById(R.id.parar);
        fechar.setOnClickListener(this);
        afd = this.getResources().openRawResourceFd(R.drawable.alarmclock);
        incrementaSeg();
    }


	@Override
	public void onClick(View v) {
		 Bundle b = new Bundle();
         Message m = new Message();
         m.what = PARAR ;
         m.setData(b);
         handler.sendMessage(m);
	}
	
	
	
	 public Handler handler = new Handler(){
	    	
	    	public void handleMessage(Message m) {
				switch (m.what) {
				
				case DECREMENTAR:
					cronometro.setText(m.getData().getString("resultado"));  
					break;
					
				case PARAR:
					continuar = false;
					tempoMin = 0;
					tempoSeg = 0;
					break;

				default:
					break;
				}
			}
	 };
	 
	 public void incrementaSeg(){
	        
	    	new Thread(){
	    		public void run() {
	    			while((tempoMin > 0 || tempoSeg > 0) && continuar ){
	    				if(tempoSeg > 0){
	    					tempoSeg--;
	    				}else{
	    					tempoMin--;
	    					tempoSeg = 59;
	    				}
	    	              
	    	              String segundosString = "";
	    	              
	    	              if(tempoSeg< 10){
	    	            	  segundosString+= "0"+ tempoSeg;
	    	              }else{
	    	            	  segundosString+=  tempoSeg;
	    	              }
	    	              
	    	              
	    	              try{               
	    	                  Thread.sleep(1000);
	    	                  Bundle b = new Bundle();
	    	                  b.putString("resultado",tempoMin+":"+segundosString );
	    	                  Message m = new Message();
	    	                  m.what = DECREMENTAR ;
	    	                  m.setData(b);
	    	                  handler.sendMessage(m);
	    	              }catch (Exception e) {               
	    	            }
	    	          } 
	    			 tocarBip();
				}
	    	}.start();   
	    }  
	    
	    public void tocarBip(){      
	    	tocarMp3();
	    	toast.show();
	    	Intent it = new Intent(this,ContadorZ2Activity.class);
	    	try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Log.d("[contadorz2]", "Acabou o tempo");
			finish();
	    	startActivity(it); 
	    	mediaPlayer.stop();
	    }
	    
	    public void tocarMp3(){
	    	mediaPlayer = new MediaPlayer();
	    	try
	        {  
	            mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
	            mediaPlayer.setVolume(13, 13);
	            mediaPlayer.prepare();
	            mediaPlayer.start();
	            afd.close();
	        }
	        catch (IllegalArgumentException e)
	        {
	            Log.e("[contadorz2]", "Unable to play audio queue do to exception: " + e.getMessage(), e);
	        }
	        catch (IllegalStateException e)
	        {
	            Log.e("[contadorz2]", "Unable to play audio queue do to exception: " + e.getMessage(), e);
	        }
	        catch (IOException e)
	        {
	            Log.e("[contadorz2]", "Unable to play audio queue do to exception: " + e.getMessage(), e);
	        }
	    }
	    @Override
	    protected void onPause() {
	    	super.onDestroy();
	    }
}

